<?php
session_start();
//数据过滤函数库
/*
功能：转码and用来过滤字符串和字符串数组，防止被挂马和sql注入 
参数$data，待过滤的字符串或字符串数组，
$force为true，忽略get_magic_quotes_gpc
*/
function in($data,$force=false)
{
	if(is_string($data))
	{
		$data=trim(htmlspecialchars($data));//防止被挂马，跨站攻击
		if(($force==true)||(!get_magic_quotes_gpc())) 
		{
		   $data = addslashes($data);//防止sql注入
		}
		return  $data;
	}
	else if(is_array($data))//如果是数组采用递归过滤
	{
		foreach($data as $key=>$value)
		{
			 $data[$key]=in($value);
		}
		return $data;
	}
	else 
	{
		return $data;
	}	
}

//用来还原字符串和字符串数组，把已经转义的字符还原回来
function out($data)
{
	if(is_string($data))
	{
		return $data = stripslashes($data);
	}
	else if(is_array($data))//如果是数组采用递归过滤
	{
		foreach($data as $key=>$value)
		{
			 $data[$key]=out($value);
		}
		return $data;
	}
	else 
	{
		return $data;
	}	
}

//文本输入
function text_in($str)
{
	$str=strip_tags($str,'<br>');
	$str = str_replace(" ", "&nbsp;", $str);
	$str = str_replace("\n", "<br>", $str);	
	if(!get_magic_quotes_gpc()) 
	{
  	  $str = addslashes($str);
	}
	return $str;
}

//文本输出
function text_out($str)
{
	$str = str_replace("&nbsp;", " ", $str);
	$str = str_replace("<br>", "\n", $str);	
    $str = stripslashes($str);
	return $str;
}

//html代码输入
function html_in($str)
{
	$search = array ("'<script[^>]*?>.*?</script>'si",  // 去掉 javascript
					 "'<iframe[^>]*?>.*?</iframe>'si", // 去掉iframe
					);
	$replace = array ("",
					  "",
					);			  
   $str=@preg_replace ($search, $replace, $str);
   $str=htmlspecialchars($str);
   	if(!get_magic_quotes_gpc()) 
	{
  	  $str = addslashes($str);
	}
   return $str;
}

//html代码输出
function html_out($str)
{
	if(function_exists('htmlspecialchars_decode'))
		$str=htmlspecialchars_decode($str);
	else
		$str=html_entity_decode($str);

    $str = stripslashes($str);
	return $str;
}
//json转码utf-8
	function myjson($code)
	{
		if(is_array($code))
		{
			foreach($code as $key=>$val)
			{
			$code[$key] = urlencodeAry($code);
			}
			return $code;
		}else{
			return urlencodeAry($code);
		}
	}
	
	function urlencodeAry($code){
	
		$code = json_encode($code);

		$code=preg_replace("#\\\u([0-9a-f]+)#ie", "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))", $code);

		return	 str_replace('\\','',$code);
	}

	
	// 获取客户端IP地址
	function get_client_ip(){
	   if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
		   $ip = getenv("HTTP_CLIENT_IP");
	   else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
		   $ip = getenv("HTTP_X_FORWARDED_FOR");
	   else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
		   $ip = getenv("REMOTE_ADDR");
	   else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
		   $ip = $_SERVER['REMOTE_ADDR'];
	   else
		   $ip = "unknown";
	   return $ip;
	}

	//中文字符串截取
	function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
	{
		switch($charset)
		{
			case 'utf-8':$char_len=3;break;
			case 'UTF8':$char_len=3;break;
			default:$char_len=2;
		}
		//小于指定长度，直接返回
		if(strlen($str)<=($length*$char_len))
		{	
			return $str;
		}
		if(function_exists("mb_substr"))
		{   
			$slice= mb_substr($str, $start, $length, $charset);
		}
		else if(function_exists('iconv_substr'))
		{
			$slice=iconv_substr($str,$start,$length,$charset);
		}
		else
		{ 
		   $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
			$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
			$re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
			$re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
			preg_match_all($re[$charset], $str, $match);
			$slice = join("",array_slice($match[0], $start, $length));
		}
		if($suffix) 
			return $slice."…";
		return $slice;
	}

	//检查是否是正确的邮箱地址，是则返回true，否则返回false
	function is_email($user_email)
	{
		
		if(!empty($user_email))
		{
			$chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
			if (strpos($user_email, '@') !== false && strpos($user_email, '.') !== false)
			{
				if (preg_match($chars, $user_email))
				{
					$query = mysql_query("SELECT * FROM `food_user_basic` WHERE `user_mail` = '".$user_email."'");
					$num = mysql_num_rows($query);
					if($num == 0)
					{
						return true;
						
					}else
					{
						echo "&#12288;邮件已被注册";
						return false;
					}
					
					
				}else
				{
					echo "&#12288;邮件格式错误";
					return false;
				}
			}else
			{
				echo "&#12288;邮件格式错误";
				return false;
			}
		}else
		{
			echo "&#12288;邮箱不能为空！";
			return false;
		}
	}
	
	//检查密码
	function is_password($user_pass)
	{
		if(!empty($user_pass))
		{
			if(strlen($user_pass) < 9)
			{
				return true;
			}
			else
			{
				echo "&#12288;密码不能超过8位数!";
				return false;
			}
		}
		else
		{
			echo "&#12288;密码不能为空!";
			return false;
		}
	}
	
	//用户名验证
	function is_nick($nick)
	{
		if(!empty($nick))
		{
			$query = mysql_query("SELECT * FROM `food_user_basic` WHERE `nick` = '".$nick."' ORDER BY `id` DESC");
			$num = mysql_num_rows($query);
			if($num == 0)
			{
				if(strlen($nick) < 19)
				{
					return true;
				}
				else
				{
					echo "&#12288;用户名不能超过6个字!";
					return false;
				}
			}else
			{
				echo "&#12288;用户名已被占用!";
				return false;
			}
			
		}
		else
		{
			echo "&#12288;用户名不能为空!";
			return false;
		}
	}
	
	//验证码验证
	function is_yzm($yzm)
	{
		if(!empty($yzm))
		{
			if($_SESSION['check'] == $yzm)
			{
				return true;
			}
			else
			{
				echo "&#12288;验证码输入错误!";
				return false;
			}
		}
		else
		{
			echo "&#12288;验证码不能为空!";
			return false;
		}
	}

	
	
	//检查是否是正确的邮箱地址，是则返回true，否则返回false
	function is_email_reg($user_email)
	{
		
		if(!empty($user_email))
		{
			$chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
			if (strpos($user_email, '@') !== false && strpos($user_email, '.') !== false)
			{
				if (preg_match($chars, $user_email))
				{
					$query = mysql_query("SELECT * FROM `food_user_basic` WHERE `user_mail` = '".$user_email."'");
					$num = mysql_num_rows($query);
					if($num == 0)
					{
						return true;
						
					}else
					{
						return false;
					}
					
					
				}else
				{
					return false;
				}
			}else
			{
				return false;
			}
		}else
		{
			return false;
		}
	}
	
	//检查密码
	function is_password_reg($user_pass)
	{
		if(!empty($user_pass))
		{
			if(strlen($user_pass) < 9)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	
	//用户名验证
	function is_nick_reg($nick)
	{
		if(!empty($nick))
		{
			$query = mysql_query("SELECT * FROM `food_user_basic` WHERE `nick` = '".$nick."' ORDER BY `id` DESC");
			$num = mysql_num_rows($query);
			if($num == 0)
			{
				if(strlen($nick) < 19)
				{
					return true;
				}
				else
				{
					echo "&#12288;用户名不能超过6个字!";
					return false;
				}
			}else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	
	//验证码验证
	function is_yzm_reg($yzm)
	{
		if(!empty($yzm))
		{
			if($_SESSION['check'] == $yzm)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}

	
	
  myjson("&#25991;&#65288;&#35774;&#35745;&#65289;&#39064;&#30446;");
?>